<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查询数据库</title>
<style type="text/css">
h1{ color:red; }
</style>
</head>
<body>
<h1>显示成语数据库的前6条记录</h1><% 
//(1)加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//(2)建立连接\创建语句   try-with-resources来关闭资源，避免资源泄露
try(Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "root", "12qwas");
	Statement stat=cn.createStatement()){
	try(ResultSet rs=stat.executeQuery("select cy as chyu from chengyu limit 6")){//执行查询获得结果集
		//(5)遍历结果集所有记录并显示
		while(rs.next()){ //若还有记录，就一直循环
			//String cy=rs.getString(1);//columnIndex, 索引号从1开始
			String cy=rs.getString("chyu"); //columnLabel
			out.println("<p>"+cy+"</p>");
		}
	}
}
%>

</body>
</html>